package com.luhuiguo.contacts.client.ui;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HasValue;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;
import com.luhuiguo.contacts.client.AppConstants;
import com.luhuiguo.contacts.client.AppMessages;
import com.luhuiguo.contacts.client.AppValidationMessages;
import com.luhuiguo.contacts.client.presenter.EditContactPresenter;
import com.luhuiguo.gwt.validation.client.DefaultValidationProcessor;
import com.luhuiguo.gwt.validation.client.ValidationProcessor;
import com.luhuiguo.gwt.validation.client.actions.FocusAction;
import com.luhuiguo.gwt.validation.client.actions.StyleAction;
import com.luhuiguo.gwt.validation.client.description.PopupDescription;
import com.luhuiguo.gwt.validation.client.description.PopupDescription.Location;
import com.luhuiguo.gwt.validation.client.validators.strings.StringLengthValidator;

public class EditContactView extends DialogBox implements EditContactPresenter.Display {

    private static EditContactViewUiBinder uiBinder = GWT.create(EditContactViewUiBinder.class);

    interface EditContactViewUiBinder extends UiBinder<Widget, EditContactView> {
    }

    @SuppressWarnings("unused")
    private AppConstants constants;

    @SuppressWarnings("unused")
    private AppMessages messages;

    private ValidationProcessor validator;

    private PopupDescription description;

    @UiField
    TextBox company;

    @UiField
    TextBox name;

    @UiField
    TextBox phone;

    @UiField
    TextArea note;

    @UiField
    Button submit;

    @UiField
    Button cancel;

    @Inject
    public EditContactView(AppConstants constants, AppMessages messages, AppValidationMessages validationMessages) {

        this.constants = constants;
        this.messages = messages;

        setModal(true);
        setAnimationEnabled(true);
        // setGlassEnabled(true);
        setText(constants.editContactDialogCaption());

        setWidget(uiBinder.createAndBindUi(this));

        //AppValidationMessages validationMessages = new AppValidationMessages(constants, messages);
        validator = new DefaultValidationProcessor(validationMessages);
        description = new PopupDescription(validationMessages, Location.RIGHT);

        description.addDescription("companyDesc", company);
        description.addDescription("nameDesc", name);
        description.addDescription("phoneDesc", phone);
        description.addDescription("noteDesc", note);

        FocusAction fAction = new FocusAction();
        StyleAction sAction = new StyleAction(constants.validationFailedBorder());
        validator.addValidators("company",
                new StringLengthValidator(company, 1, 100).addActionForFailure(fAction).addActionForFailure(sAction))
                .addValidators(
                        "name",
                        new StringLengthValidator(name, 1, 50).addActionForFailure(fAction)
                                .addActionForFailure(sAction)).addValidators(
                        "phone",
                        new StringLengthValidator(phone, 1, 50).addActionForFailure(fAction).addActionForFailure(
                                sAction)).addValidators(
                        "note",
                        new StringLengthValidator(note, 0, 250).addActionForFailure(fAction).addActionForFailure(
                                sAction));
        GWT.log("EditContactView created");
    }

    @Override
    public Widget asWidget() {
        return this;
    }

    @Override
    public HasValue<String> getCompany() {
        return company;
    }

    @Override
    public HasValue<String> getName() {
        return name;
    }

    @Override
    public HasValue<String> getNote() {
        return note;
    }

    @Override
    public HasValue<String> getPhone() {
        return phone;
    }

    @Override
    public HasClickHandlers getSubmit() {
        return submit;
    }

    @Override
    public HasClickHandlers getCancel() {
        return cancel;
    }

    @Override
    public boolean validate() {
        return validator.validate();
    }

    @Override
    public ValidationProcessor getValidator() {
        return validator;
    }

    @Override
    public void setLoading(boolean loading) {
        if (loading) {
            // loading.show();
            submit.setEnabled(false);
            cancel.setEnabled(false);
        } else {
            // loading.hide();
            submit.setEnabled(true);
            cancel.setEnabled(true);
        }

    }

}
